package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ProductUtils;
import com.ruoyi.system.domain.GuiGeVo;
import com.ruoyi.system.domain.TGoodsSpecification;
import com.ruoyi.system.domain.ValueVo;
import com.ruoyi.system.mapper.TGoodsSpecificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TGoodsMapper;
import com.ruoyi.system.domain.TGoods;
import com.ruoyi.system.service.ITGoodsService;

import javax.annotation.Resource;

/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-08
 */
@Service
public class TGoodsServiceImpl implements ITGoodsService 
{
    @Resource
    private TGoodsMapper tGoodsMapper;

    @Resource
    private TGoodsSpecificationMapper tGoodsSpecificationMapper;
    /**
     * 查询商品
     * 
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public TGoods selectTGoodsById(Long id)
    {
        List<TGoodsSpecification> list = tGoodsSpecificationMapper.selectTGoodsSpecificationByProductId(id);
        TGoods tGoods = tGoodsMapper.selectTGoodsById(id);
        List<GuiGeVo> list1 = get(list);
        tGoods.setSpecificationsList(list1);
        tGoods.setSpecifications(list);
        return tGoods;
    }

    /**
     * 查询商品列表
     * 
     * @param tGoods 商品
     * @return 商品
     */
    @Override
    public List<TGoods> selectTGoodsList(TGoods tGoods)
    {
        List<TGoods> tGoods1 = tGoodsMapper.selectTGoodsList(tGoods);
        for (TGoods goods : tGoods1) {
            List<TGoodsSpecification> tGoodsSpecifications = tGoodsSpecificationMapper.selectTGoodsSpecificationByProductId(goods.getId());
            List<GuiGeVo> list = get(tGoodsSpecifications);
            goods.setSpecificationsList(list);
            goods.setSpecifications(tGoodsSpecifications);
        }
        return tGoods1;
    }

    /**
     * 新增商品
     * 
     * @param tGoods 商品
     * @return 结果
     */
    @Override
    public int insertTGoods(TGoods tGoods)
    {
        Long productId = ProductUtils.getProductId();
        List<TGoodsSpecification> specifications = tGoods.getSpecifications();
        for (TGoodsSpecification specification : specifications) {
            specification.setProductId(productId);
            tGoodsSpecificationMapper.insertTGoodsSpecification(specification);
        }
        tGoods.setId(productId);
        tGoods.setCreateTime(DateUtils.getNowDate());
        return tGoodsMapper.insertTGoods(tGoods);
    }

    /**
     *
     * 修改商品
     * 
     * @param tGoods 商品
     * @return 结果
     */
    @Override
    public int updateTGoods(TGoods tGoods)
    {
        tGoods.setUpdateTime(DateUtils.getNowDate());
        return tGoodsMapper.updateTGoods(tGoods);
    }

    /**
     * 批量删除商品
     * 
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteTGoodsByIds(Long[] ids)
    {
        return tGoodsMapper.deleteTGoodsByIds(ids);
    }

    /**
     * 删除商品信息
     * 
     * @param id 商品主键
     * @return 结果
     */
    @Override
    public int deleteTGoodsById(Long id)
    {
        return tGoodsMapper.deleteTGoodsById(id);
    }


    public List<GuiGeVo> get(List<TGoodsSpecification> tGoodsSpecifications){
        Map<String,List<ValueVo>> map = new HashMap<>();
        for (TGoodsSpecification tGoodsSpecification : tGoodsSpecifications) {
            ValueVo valueVo = new ValueVo();
            if (map.containsKey(tGoodsSpecification.getName())){
                List<ValueVo> list1 = map.get(tGoodsSpecification.getName());
                valueVo.setId(tGoodsSpecification.getId());
                valueVo.setName(tGoodsSpecification.getValue());
                list1.add(valueVo);
                map.put(tGoodsSpecification.getName(),list1);
            }else {
                List<ValueVo> list = new ArrayList<>();
                valueVo.setId(tGoodsSpecification.getId());
                valueVo.setName(tGoodsSpecification.getValue());
                list.add(valueVo);
                map.put(tGoodsSpecification.getName(),list);
            }
        }
        List<GuiGeVo> list = new ArrayList<>();
        Set<String> strings = map.keySet();
        for (String string : strings) {
            GuiGeVo guiGeVo =new  GuiGeVo();
            List<ValueVo> valueVos = map.get(string);
            guiGeVo.setName(string);
            guiGeVo.setValue(valueVos);
            list.add(guiGeVo);
        }
        return list;
    }
}
